package com;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import model.Order;
import model.Product;

public class Util {

	private static String encoding = "text/html; charset=UTF-8";

	private static Properties buildMailConfig() {
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");

		return props;
	}

	public static void sendMail(String to, Order order) {

		Properties props = buildMailConfig();

		Session session = Session.getDefaultInstance(props, new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("teal.project", "Mallard777");
			}
		});

		try {
			Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress("chirok"));
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
			msg.addRecipient(Message.RecipientType.BCC, new InternetAddress("sergei.bardas@gmail.com"));
			msg.setSubject("Ваш заказ №" + order.getOrderId());

			Multipart multipart = new MimeMultipart();

			Iterator it = order.getProduct().iterator();

			String mailBody = "Здравствуйте,Ваш заказ:<br/>";
			BigDecimal subTotal = BigDecimal.ZERO;

			BodyPart bodyPart = new MimeBodyPart();

			while (it.hasNext()) {
				Product p = (Product) it.next();
				mailBody += "&nbsp;" + p.getName() + "&nbsp;-&nbsp;" + p.getTotalPrice() + "грн.(" + p.getCount()
						+ "шт.)<br/>";
				subTotal = subTotal.add(p.getTotalPrice());
			}
			mailBody += "всего:<b>" + subTotal + "грн.</b><br/><br/>";
			mailBody += "В ближайшее время мы свяжемся с Вами,чтобы уточнить вопросы доставки.<br/>";
			mailBody += "----------<br/>";
			mailBody += "С уважением,<br/><a href='http:/www.chirok.com.ua'>chirok.com.ua</a>";
			bodyPart.setContent(mailBody, encoding);

			multipart.addBodyPart(bodyPart);
			msg.setContent(multipart, encoding);

			Transport.send(msg);
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}

	public static void sendMail(String username, String to, String confirmationCode) {

		Properties props = buildMailConfig();

		Session session = Session.getDefaultInstance(props, new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("teal.project", "Mallard777");
			}
		});
		try {
			Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress("chirok"));
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
			msg.addRecipient(Message.RecipientType.BCC, new InternetAddress("sergei.bardas@gmail.com"));

			Multipart multipart = new MimeMultipart();

			BodyPart bodyPart = new MimeBodyPart();

			bodyPart.setContent("Your confirmation link is:<br/><a href='http://chirok.com.ua/confirmed.xhtml?username="
					+ username + "&code=" + confirmationCode + ">", encoding);

			multipart.addBodyPart(bodyPart);
			msg.setContent(multipart, encoding);

			Transport.send(msg);

		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}

	}
}
